package org.example;

/**
 * Hello world!
 *
 */

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//E:\\CodeWorkSpace\\Gitee\\React\\JasperReports\\JR_Demo
//E:\CodeWorkSpace\Gitee\React\JasperReports\JR_Demo

public class JasperReportsTest {

    @Test
    public void demo() throws JRException {
        String jrxmlPath = "E:\\CodeWorkSpace\\Gitee\\React\\JasperReports\\JR_Demo\\src\\main\\resources\\demo.jrxml";
        String jasperPath = "E:\\CodeWorkSpace\\Gitee\\React\\JasperReports\\JR_Demo\\src\\main\\resources\\demo.jasper";

        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

        //构造数据
        Map paramters = new HashMap();
        paramters.put("reportDate","2019-10-10");
        paramters.put("company","itcast");
        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("name","xiaoming");
        map1.put("address","beijing");
        map1.put("email","xiaoming@itcast.cn");
        Map map2 = new HashMap();
        map2.put("name","xiaoming");
        map2.put("address","beijing");
        map2.put("email","xiaoming@itcast.cn");
        list.add(map1);
        list.add(map2);

        //填充数据
        JasperPrint jasperPrint =
        JasperFillManager.fillReport(jasperPath,paramters, new JRBeanCollectionDataSource(list));
        //输出文件
        String pdfPath = "D:\\TestDemo.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
    }


    //基于JDBC数据源
    @Test
    public void demo2() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");

        String jrxmlPath = "E:\\CodeWorkSpace\\Gitee\\React\\JasperReports\\JR_Demo\\src\\main\\resources\\demo2.jrxml";
        String jasperPath = "E:\\CodeWorkSpace\\Gitee\\React\\JasperReports\\JR_Demo\\src\\main\\resources\\demo2.jasper";

        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);

        //构造数据
        HashMap parmters = new HashMap();
        parmters.put("company", "传智博客");

        //填充数据---使用JDBC数据源方式填充
        JasperPrint jasperPrint
                = JasperFillManager.fillReport(jasperPath, parmters, connection);

        //输出文件
        String pdfPath = "D:\\TestDemo2.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);

    }
}



